package dev.pocketpal;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class HatchingActivity extends Activity {

	final String ACTIVITY_NAME = "HatchingActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.i(ACTIVITY_NAME, "Launching hatching page");
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_hatching);

		// Fire Egg Button Event
		eggSelection(R.id.fireEggButton, R.drawable.fire_egg);
		// Water Egg Button Event
		eggSelection(R.id.waterEggButton, R.drawable.water_egg);
		// Earth Egg Button Event
		eggSelection(R.id.earthEggButton, R.drawable.earth_egg);
		// Air Egg Button Event
		eggSelection(R.id.airEggButton, R.drawable.air_egg);

	}

	/**
	 * eggSelection 
	 * 
	 * Creates a button click event which displays a dialog that
	 * will allows the user to confirm or cancel their choice of the egg.
	 * If the user confirms, show hatching animation?
	 * 
	 * @param viewId
	 *            ID of the ImageButton
	 * @param drawableId
	 * 			  ID of the egg selected
	 */
	private void eggSelection(int viewId, int drawableId) {
		final Drawable drawing = getResources().getDrawable(drawableId);
		ImageButton bt = (ImageButton) findViewById(viewId);
		bt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				final String eggType = (String) view.getContentDescription();
				Log.i(ACTIVITY_NAME, "Choosing " + eggType + " Egg");

				// Create the dialog
				final Dialog dialog = new Dialog(HatchingActivity.this);
				dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.setContentView(R.layout.popup_hatching);
				
				// Define the text and image
				TextView text = (TextView) dialog.findViewById(R.id.hPopupText);
				text.setText("You have chosen the " + eggType + " Egg. Are you sure?");
				ImageView image = (ImageView) dialog.findViewById(R.id.hPopupImage);
				image.setImageDrawable(drawing);
				
				// Confirmation button
				Button dialogConfirm = (Button) dialog
						.findViewById(R.id.hPopupYesButton);
				dialogConfirm.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						Log.i(ACTIVITY_NAME, eggType + " Egg Confirmed");
						dialog.dismiss();
					}
				});

				// Cancellation button
				Button dialogCancel = (Button) dialog
						.findViewById(R.id.hPopupNoButton);
				dialogCancel.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {
						Log.i(ACTIVITY_NAME, eggType + " Egg Rejected");
						dialog.dismiss();
					}
				});

				dialog.show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.hatching, menu);
		return true;
	}

}
